﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
using QLKH.PopUp.CongNo;

namespace QLKH.View.congno
{
    public partial class dsCongNoql : UserControl
    {
        DataView view1 = new DataView();
        public dsCongNoql()
        {
            InitializeComponent();
        }

        public void capnhatrano(int id)
        {
            clCongNo clcongno = new clCongNo();
            DataTable dtcongno = clcongno.danhSachCongNo();

            dataGridViewCongNo1.AutoGenerateColumns = false;
            dataGridViewCongNo1.DataSource = view1 = dtcongno.DefaultView;
        }

        private void dsCongNoql_Load(object sender, EventArgs e)
        {
            clCongNo clcn = new clCongNo();
            DataTable cldt = clcn.danhSachCongNo();

            dataGridViewCongNo1.AutoGenerateColumns = false;
            dataGridViewCongNo1.DataSource = view1 = cldt.DefaultView;
        }

        private void dataGridViewCongNo1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            lbRowIndex.Text = e.RowIndex.ToString();
        }

        private void tool_trano_Click(object sender, EventArgs e)
        {
            int rowcn = int.Parse(lbRowIndex.Text.Trim());
            if (rowcn >= 0)
            {
                Int64 idkh = Int64.Parse(dataGridViewCongNo1.Rows[rowcn].Cells["idkh"].Value.ToString());
                Int64 thangnam = Int64.Parse(dataGridViewCongNo1.Rows[rowcn].Cells["namNo"].Value.ToString() + dataGridViewCongNo1.Rows[rowcn].Cells["thangNo"].Value.ToString());
                CapNhatSoTienTra usfitem = new CapNhatSoTienTra(idkh,thangnam);
                usfitem.MyFunction = new CapNhatSoTienTra.capnhatcongno(capnhatrano);
                usfitem.ShowDialog();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn khách hàng");
            }
        }

        private void dataGridViewCongNo1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridViewCongNo1.Rows)
            {
                if (Int16.Parse(row.Cells["trangThai"].Value.ToString()) == 1)
                {
                    row.DefaultCellStyle.BackColor = Color.SkyBlue;
                }
            }
        }

        private void tool_delete_Click(object sender, EventArgs e)
        {
            int row = int.Parse(lbRowIndex.Text.Trim());
            if (row >= 0)
            {
                Int64 idkh = Int64.Parse(dataGridViewCongNo1.Rows[row].Cells["idkh"].Value.ToString());
                Int64 thangnam = Int64.Parse(dataGridViewCongNo1.Rows[row].Cells["namNo"].Value.ToString() + dataGridViewCongNo1.Rows[row].Cells["thangNo"].Value.ToString());
                clCongNo clkh = new clCongNo();

                if (Int16.Parse(dataGridViewCongNo1.Rows[row].Cells["trangThai"].Value.ToString()) == 0)
                {
                    DialogResult result = MessageBox.Show("Bạn muốn xóa công nợ của khách hàng này?", "Thông báo", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        clkh.xoacongno(idkh, thangnam);

                        DataTable cldt = clkh.danhSachCongNo();

                        dataGridViewCongNo1.AutoGenerateColumns = false;
                        dataGridViewCongNo1.DataSource = view1 = cldt.DefaultView;
                    }
                }
                else {
                    MessageBox.Show("Khách hàng này vẫn còn nợ. Bạn không được xóa");
                }
            }
        }
    }
}
